package com.dtw.ditouwang.ui.more;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommentDetialBean;
import com.dtw.ditouwang.beans.CommonBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommentMoreActivity extends BaseActivity implements TextView.OnEditorActionListener, OnItemClickListener, View.OnClickListener {
    public final static String KeyCommentID = "KeyCommentID";
    public final static String KeyArtID = "KeyArtID";
    private CircleImageView imgHead;
    private TextView textName;
    private TextView textContent;
    private TextView timeText;
    EditText editText;
    private RecyclerView recyclerview;
    List<CommentDetialBean.DataBean.SecondBean> commentMoreList = new ArrayList<>();
    CommentMoreAdapter commentMoreAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_more);
        initView();
        enableBack();
        setTitle("回复详情");
        editText = findViewById(R.id.text_send_comment);
        editText.setOnEditorActionListener(this);
        commentMoreAdapter = new CommentMoreAdapter(commentMoreList);
        commentMoreAdapter.setOnItemClickListener(this);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(commentMoreAdapter);
        findViewById(R.id.sendButton).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        webApi.getCommentDetial(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyCommentID, 0)).enqueue(new Callback<CommentDetialBean>() {
            @Override
            public void onResponse(Call<CommentDetialBean> call, Response<CommentDetialBean> response) {
                if (response.body().isResult() && !isFinishing()) {
                    textName.setText(response.body().getData().getComment().getNickname());
                    textContent.setText(response.body().getData().getComment().getC_info());
                    timeText.setText(response.body().getData().getComment().getComment_timee());
                    if (!TextUtils.isEmpty(response.body().getData().getComment().getHead())) {
                        Glide.with(imgHead).load(WebApi.BaseUrl + response.body().getData().getComment().getHead().replace("\\", "/")).into(imgHead);
                    }

                    commentMoreList.clear();
                    commentMoreList.addAll(response.body().getData().getSecond());
                    commentMoreAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<CommentDetialBean> call, Throwable t) {

            }
        });
    }

    private void initView() {
        timeText = findViewById(R.id.text_time);
        imgHead = findViewById(R.id.img_head);
        textName = findViewById(R.id.text_name);
        textContent = findViewById(R.id.text_content);
        recyclerview = findViewById(R.id.recyclerview);
    }

    @Override
    public boolean onEditorAction(final TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            findViewById(R.id.sendButton).callOnClick();
        }
        return false;
    }

    @Override
    public void onItemClick(final int resID, int position, int state) {
        webApi.setAwesomeForComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyArtID, 0),
                commentMoreList.get(position).getId(), getIntent().getIntExtra(KeyCommentID, 0), state).enqueue(new Callback<CommonBean>() {
            @Override
            public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                if (!response.body().isResult()) {
                    showToast(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<CommonBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sendButton:
                webApi.sendSecondComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyArtID, 0),
                        getIntent().getIntExtra(KeyCommentID, 0), editText.getText().toString()).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (response.body().isResult()) {
                            showToast("评论成功");
                            editText.setText(null);
                        } else {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
        }
    }
}
